
*NOTE: Please ensure you have copied the Stata files from the ado folder included with this replication package

************************************
*** FIGURE 2: Dynamic Effect of EMR Adoption on log(Patient Deaths)
*** FIGURE 2: Dynamic Effect of EMR Adoption on log(Patient Deaths)
*** FIGURE 2: Dynamic Effect of EMR Adoption on log(Patient Deaths)
*** FIGURE 2: Dynamic Effect of EMR Adoption on log(Patient Deaths)
*** FIGURE A5: EMR Treatment Effect on New Patients, Alternative Estimators 

*** TABLE A5: Dynamic Effect of EMR Adoption on Number of Patient Deaths
*** TABLE A9: Dynamic Effect of EMR Adoption on Number of Patients In Care
*** TABLE A10: Dynamic Effect of EMR Adoption on Number of New Patients
*** TABLE A11: Dynamic Effect of EMR Adoption on Patient Retention
************************************


use Data/Analysis/Data_ClinicYears, clear


keep YearsPostEMR EMRyear Datey VisitSite logInCare* logDeath* logNew* logReturn* Post hospital urban large

gen Ei=EMRyear
gen t=Datey
gen K = t-Ei 								
gen D = K>=0 & Ei!=. 

gen i=VisitSite
tsset i t


// TWFE 

forvalues l = 0/6 {
	gen L`l'event = K==`l'
}
replace L6event=1 if K>6
forvalues l = 1/6 {
	gen F`l'event = K==-`l'
}
drop F1event


cap erase Output/Tables/TableA05.xls
cap erase Output/Tables/TableA05.txt

foreach y of varlist logDeath {
	foreach x of varlist `y' `y'Male `y'Female `y'Age* {
		reghdfe `x' F*event L*event, a(i t) cluster(i)
			estimates store e_`x'
			testparm F*event
			outreg2 using Output/Tables/TableA05.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(F*event L*event) addtext(Year FE, Y, Clinic FE, Y) addstat("F-test on pre-trends", r(F),"p-value of F-test", r(p))
	}
}

foreach y of varlist logDeath {
	event_plot e_`y' e_`y'Male e_`y'Female, ///
		stub_lag(L#event L#event L#event) stub_lead(F#event F#event F#event) plottype(scatter) ciplottype(rspike) ///
	together perturb(-0.375(0.2)0.375) trimlead(6) noautolegend ///
	graph_opt( ///
			xtitle("Year Index") ytitle("Coefficient Estimates") ///
					xlabel(-6 "-6" -5 "-5" -4 "-4" -3 "-3" -2 "-2" -1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-1(0.5)0.5) ///
			legend(pos(6) order(1 "All" 3 "Male" ///
					5 "Female") rows(1) region(style(none))) ///
		/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(O) color(black)) lag_ci_opt1(color(black)) ///
		lag_opt2(msymbol(T) color(green)) lag_ci_opt2(color(green)) ///
		lag_opt3(msymbol(S) color(red)) lag_ci_opt3(color(red)) 
	graph save "Output/Figures/Figure02PanelA.gph", replace
	graph export "Output/Figures/Figure02PanelA.png", replace
}

foreach y of varlist logDeath {
	event_plot e_`y'Age_09 e_`y'Age_1017 e_`y'Age_1849 e_`y'Age_50plus, ///
	stub_lag(L#event L#event L#event) stub_lead(F#event F#event F#event) plottype(scatter) ciplottype(rspike) ///
	together perturb(-0.3(0.2)0.3) trimlead(6) noautolegend ///
	graph_opt( ///
		xtitle("Year Index") ytitle("Coefficient Estimates") ///
		xlabel(-6 "-6" -5 "-5" -4 "-4" -3 "-3" -2 "-2" -1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-2(1)3) ///
		legend(pos(6) order(1 "0-9" 3 "10-17" ///
				5 "18-49" 7 "50+") rows(1) region(style(none))) ///
	/// the following lines replace default_look with something more elaborate
		xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
	) ///
		lag_opt1(msymbol(O) color(blue)) lag_ci_opt1(color(blue)) ///
		lag_opt2(msymbol(D) color(orange)) lag_ci_opt2(color(orange)) ///
		lag_opt3(msymbol(T) color(green)) lag_ci_opt3(color(green)) ///
		lag_opt4(msymbol(S) color(red)) lag_ci_opt4(color(red)) 
	graph save "Output/Figures/Figure02PanelB.gph", replace
	graph export "Output/Figures/Figure02PanelB.png", replace
}


cap erase Output/Tables/TableA10.xls
cap erase Output/Tables/TableA10.txt

foreach y of varlist logNew {
	foreach x of varlist `y' `y'Male `y'Female `y'Age* {
		reghdfe `x' F*event L*event, a(i t) cluster(i)
			estimates store e_`x'
			testparm F*event
			outreg2 using Output/Tables/TableA10.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(F*event L*event) addtext(Year FE, Y, Clinic FE, Y) addstat("F-test on pre-trends", r(F),"p-value of F-test", r(p))
	}
}

cap erase Output/Tables/TableA09.xls
cap erase Output/Tables/TableA09.txt


foreach y of varlist logInCare {
	foreach x of varlist `y' `y'Male `y'Female `y'Age* {
		reghdfe `x' L*event, a(i t) cluster(i)
			estimates store e_`x'
			outreg2 using Output/Tables/TableA09.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(L*event) addtext(Year FE, Y, Clinic FE, Y, F-test on pre-trends, N/A, p-value of F-test, N/A) 
	}
}

cap erase Output/Tables/TableA11.xls
cap erase Output/Tables/TableA11.txt

foreach y of varlist logReturn {
	foreach x of varlist `y' `y'Male `y'Female `y'Age* {
		reghdfe `x' L*event, a(i t) cluster(i)
			estimates store e_`x'
			outreg2 using Output/Tables/TableA11.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(L*event) addtext(Year FE, Y, Clinic FE, Y, F-test on pre-trends, N/A, p-value of F-test, N/A) 
	}
}



foreach y of varlist logInCare {
	event_plot e_`y' e_`y'Male e_`y'Female, ///
		stub_lag(L#event L#event L#event) stub_lead() plottype(scatter) ciplottype(rspike) ///
		together perturb(-0.375(0.2)0.375) trimlead(6) noautolegend ///
		graph_opt( ///
			xtitle("Year Index") ytitle("Coefficient Estimates") ///
					xlabel(-1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-1(0.5)0.5) ///
			legend(pos(6) order(1 "All" 3 "Male" ///
					5 "Female") rows(1) region(style(none))) ///
		/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(O) color(black)) lag_ci_opt1(color(black)) ///
		lag_opt2(msymbol(T) color(green)) lag_ci_opt2(color(green)) ///
		lag_opt3(msymbol(S) color(red)) lag_ci_opt3(color(red)) 
	graph save "Output/Figures/Figure04PanelAi.gph", replace
	graph export "Output/Figures/Figure04PanelAi.png", replace
}

foreach y of varlist logInCare  {
	event_plot e_`y'Age_09 e_`y'Age_1017 e_`y'Age_1849 e_`y'Age_50plus, ///
		stub_lag(L#event L#event L#event) stub_lead() plottype(scatter) ciplottype(rspike) ///
		together perturb(-0.3(0.2)0.3) trimlead(6) noautolegend ///
		graph_opt( ///
			xtitle("Year Index") ytitle("Coefficient Estimates") ///
			xlabel(-1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-2(1)3) ///
			legend(pos(6) order(1 "0-9" 3 "10-17" ///
				5 "18-49" 7 "50+") rows(1) region(style(none))) ///
		/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(O) color(blue)) lag_ci_opt1(color(blue)) ///
		lag_opt2(msymbol(D) color(orange)) lag_ci_opt2(color(orange)) ///
		lag_opt3(msymbol(T) color(green)) lag_ci_opt3(color(green)) ///
		lag_opt4(msymbol(S) color(red)) lag_ci_opt4(color(red)) 
	graph save "Output/Figures/Figure04PanelAii.gph", replace
	graph export "Output/Figures/Figure04PanelAii.png", replace
}






foreach y of varlist logNew {
	event_plot e_`y' e_`y'Male e_`y'Female, ///
		stub_lag(L#event L#event L#event) stub_lead(F#event F#event F#event) plottype(scatter) ciplottype(rspike) ///
	together perturb(-0.375(0.2)0.375) trimlead(6) noautolegend ///
	graph_opt( ///
			xtitle("Year Index") ytitle("Coefficient Estimates") ///
					xlabel(-6 "-6" -5 "-5" -4 "-4" -3 "-3" -2 "-2" -1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-1(0.5)0.5) ///
			legend(pos(6) order(1 "All" 3 "Male" ///
					5 "Female") rows(1) region(style(none))) ///
		/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(O) color(black)) lag_ci_opt1(color(black)) ///
		lag_opt2(msymbol(T) color(green)) lag_ci_opt2(color(green)) ///
		lag_opt3(msymbol(S) color(red)) lag_ci_opt3(color(red)) 
	graph save "Output/Figures/Figure04PanelBi.gph", replace
	graph export "Output/Figures/Figure04PanelBi.png", replace
}

foreach y of varlist logNew {
	event_plot e_`y'Age_09 e_`y'Age_1017 e_`y'Age_1849 e_`y'Age_50plus, ///
	stub_lag(L#event L#event L#event) stub_lead(F#event F#event F#event) plottype(scatter) ciplottype(rspike) ///
	together perturb(-0.3(0.2)0.3) trimlead(6) noautolegend ///
	graph_opt( ///
		xtitle("Year Index") ytitle("Coefficient Estimates") ///
		xlabel(-6 "-6" -5 "-5" -4 "-4" -3 "-3" -2 "-2" -1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-2(1)3) ///
		legend(pos(6) order(1 "0-9" 3 "10-17" ///
				5 "18-49" 7 "50+") rows(1) region(style(none))) ///
	/// the following lines replace default_look with something more elaborate
		xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
	) ///
		lag_opt1(msymbol(O) color(blue)) lag_ci_opt1(color(blue)) ///
		lag_opt2(msymbol(D) color(orange)) lag_ci_opt2(color(orange)) ///
		lag_opt3(msymbol(T) color(green)) lag_ci_opt3(color(green)) ///
		lag_opt4(msymbol(S) color(red)) lag_ci_opt4(color(red)) 
	graph save "Output/Figures/Figure04PanelBii.gph", replace
	graph export "Output/Figures/Figure04PanelBii.png", replace
}



foreach y of varlist logReturn {
	event_plot e_`y' e_`y'Male e_`y'Female, ///
		stub_lag(L#event L#event L#event) stub_lead() plottype(scatter) ciplottype(rspike) ///
		together perturb(-0.375(0.2)0.375) trimlead(6) noautolegend ///
		graph_opt( ///
			xtitle("Year Index") ytitle("Coefficient Estimates") ///
					xlabel(-1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-1(0.5)0.5) ///
			legend(pos(6) order(1 "All" 3 "Male" ///
					5 "Female") rows(1) region(style(none))) ///
		/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(O) color(black)) lag_ci_opt1(color(black)) ///
		lag_opt2(msymbol(T) color(green)) lag_ci_opt2(color(green)) ///
		lag_opt3(msymbol(S) color(red)) lag_ci_opt3(color(red)) 
	graph save "Output/Figures/Figure04PanelCi.gph", replace
	graph export "Output/Figures/Figure04PanelCi.png", replace
}

foreach y of varlist logInCare  {
	event_plot e_`y'Age_09 e_`y'Age_1017 e_`y'Age_1849 e_`y'Age_50plus, ///
		stub_lag(L#event L#event L#event) stub_lead() plottype(scatter) ciplottype(rspike) ///
		together perturb(-0.3(0.2)0.3) trimlead(6) noautolegend ///
		graph_opt( ///
			xtitle("Year Index") ytitle("Coefficient Estimates") ///
			xlabel(-1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-2(1)3) ///
			legend(pos(6) order(1 "0-9" 3 "10-17" ///
				5 "18-49" 7 "50+") rows(1) region(style(none))) ///
		/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(O) color(blue)) lag_ci_opt1(color(blue)) ///
		lag_opt2(msymbol(D) color(orange)) lag_ci_opt2(color(orange)) ///
		lag_opt3(msymbol(T) color(green)) lag_ci_opt3(color(green)) ///
		lag_opt4(msymbol(S) color(red)) lag_ci_opt4(color(red)) 
	graph save "Output/Figures/Figure04PanelCii.gph", replace
	graph export "Output/Figures/Figure04PanelCii.png", replace
}

*********************************************************************************************************
***ALTERNATIVE ESTIMATORS***
*********************************************************************************************************

use Data/Analysis/Data_ClinicYears, clear

keep YearsPostEMR EMRyear Post Datey VisitSite logDeath* logNew* 

gen Ei=EMRyear
gen t=Datey
gen K = t-Ei 								
gen D = K>=0 & Ei!=. 

gen i=VisitSite
tsset i t

// Estimation with did_imputation of Borusyak et al. (2021)

foreach y of varlist logDeath logNew {
	did_imputation `y' i t Ei, horizons(0 1 2 3 4) pretrend(3) cluster(i)
	estimates store e_bjs_`y'
}


// Estimation with did_multiplegt of de Chaisemartin and D'Haultfoeuille (2020)
// Requires correct version of package from /ado folder
foreach y of varlist logDeath logNew {
	did_multiplegt `y' i t D, robust_dynamic dynamic(6) placebo(6) breps(1000) cluster(i) 
	matrix e_dd_b_`y' = e(estimates) // storing the estimates for later
	matrix e_dd_v_`y' = e(variances)
}


// Estimation with csdid of Callaway and Sant'Anna (2020)
gen gvar = cond(Ei==., 0, Ei) // group variable as required for the csdid command

foreach y of varlist logDeath logNew {
	csdid `y', ivar(i) time(t) gvar(gvar) notyet
	estat event, estore(e_cs_`y') // this produces and stores the estimates at the same time
}


// Estimation with eventstudyinteract of Sun and Abraham (2020)
sum Ei
gen lastcohort = Ei==r(max) // dummy for the latest- or never-treated cohort
forvalues l = 0/6 {
	gen L`l'event = K==`l'
}
replace L6event=1 if K>6
forvalues l = 1/6 {
	gen F`l'event = K==-`l'
}

drop F1event // normalize K=-1 to zero

foreach y of varlist logDeath logNew {
	eventstudyinteract `y' L*event F*event, vce(cluster i) absorb(i t) cohort(Ei) control_cohort(lastcohort)
	matrix e_sa_b_`y' = e(b_iw) // storing the estimates for later
	matrix e_sa_v_`y' = e(V_iw)
}


// TWFE OLS estimation 

foreach y of varlist logDeath logNew {
	reghdfe `y' F*event L*event, a(i t) cluster(i)
	estimates store e_twfe_`y' // saving the estimates for later
}



//Defining all late (2019) adopters as pure control

tab Ei
tab Post if Ei==2019

drop F*event L*event
gen Kc=K if Ei!=2019

forvalues l = 0/6 {
	gen L`l'event = Kc==`l'
}
replace L6event=1 if Kc>6
forvalues l = 1/6 {
	gen F`l'event = Kc==-`l'
}
drop F1event


foreach y of varlist logDeath logNew{
	reghdfe `y' F*event L*event, a(i t) cluster(i)
	estimates store e_c_`y'
}

local logDeathTitle "log(Deaths)"
local logNewTitle "log(New Patients)"

***MAIN FIGURE***
// Combine all plots using the stored estimates
foreach y of varlist logDeath{
	event_plot e_twfe_`y' e_c_`y' e_bjs_`y' e_dd_b_`y'#e_dd_v_`y' e_cs_`y' e_sa_b_`y'#e_sa_v_`y' , ///
		stub_lag(L#event L#event tau# Effect_# Tp# L#event) stub_lead(F#event F#event pre# Placebo_# Tm# F#event ) plottype(scatter) ciplottype(rspike) ///
		together perturb(-0.35(0.14)0.35) trimlead(6) trimlag(6) noautolegend ///
		graph_opt( ///
			xtitle("Years Since EMR") ytitle("Coefficient Estimates") ///
			xlabel(-6 "-6" -5 "-5" -4 "-4" -3 "-3" -2 "-2" -1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-1(0.5)0.5) ///
			legend(pos(6) order(1 "TWFE" 3 "TWFE (with Control)" 5 "Borusyak et al." 7 "de Chaisemartin-D'Haultfoeuille" ///
					9 "Callaway-Sant'Anna" 11 "Sun-Abraham") rows(3) region(style(none))) ///
	/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(O) color(cranberry)) lag_ci_opt1(color(cranberry)) ///
		lag_opt2(msymbol(Oh) color(cranberry)) lag_ci_opt2(color(cranberry)) ///
		lag_opt3(msymbol(Dh) color(navy)) lag_ci_opt3(color(navy)) ///
		lag_opt4(msymbol(Th) color(forest_green)) lag_ci_opt4(color(forest_green)) ///
		lag_opt5(msymbol(Sh) color(dkorange)) lag_ci_opt5(color(dkorange)) ///
		lag_opt6(msymbol(Oh) color(purple)) lag_ci_opt6(color(purple)) 
	graph save "Output/Figures/Figure02PanelC.gph", replace
	graph export "Output/Figures/Figure02PanelC.png", replace
}

foreach y of varlist logNew{
	event_plot e_twfe_`y' e_c_`y' e_bjs_`y' e_dd_b_`y'#e_dd_v_`y' e_cs_`y' e_sa_b_`y'#e_sa_v_`y' , ///
		stub_lag(L#event L#event tau# Effect_# Tp# L#event) stub_lead(F#event F#event pre# Placebo_# Tm# F#event ) plottype(scatter) ciplottype(rspike) ///
		together perturb(-0.35(0.14)0.35) trimlead(6) trimlag(6) noautolegend ///
		graph_opt( ///
			xtitle("Years Since EMR") ytitle("Coefficient Estimates") ///
			xlabel(-6 "-6" -5 "-5" -4 "-4" -3 "-3" -2 "-2" -1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6") ylabel(-1(0.5)0.5) ///
			legend(pos(6) order(1 "TWFE" 3 "TWFE (with Control)" 5 "Borusyak et al." 7 "de Chaisemartin-D'Haultfoeuille" ///
					9 "Callaway-Sant'Anna" 11 "Sun-Abraham") rows(3) region(style(none))) ///
	/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(O) color(cranberry)) lag_ci_opt1(color(cranberry)) ///
		lag_opt2(msymbol(Oh) color(cranberry)) lag_ci_opt2(color(cranberry)) ///
		lag_opt3(msymbol(Dh) color(navy)) lag_ci_opt3(color(navy)) ///
		lag_opt4(msymbol(Th) color(forest_green)) lag_ci_opt4(color(forest_green)) ///
		lag_opt5(msymbol(Sh) color(dkorange)) lag_ci_opt5(color(dkorange)) ///
		lag_opt6(msymbol(Oh) color(purple)) lag_ci_opt6(color(purple)) 
	graph save "Output/Figures/FigureA05.gph", replace
	graph export "Output/Figures/FigureA05.png", replace
}
